package cn.liulingfengyu.behavioralpatterns.interpreterpattern;

/**
 * 解释器模式示例
 */
public class InterpreterPatternDemo {
    public static void main(String[] args) {
        // 获取表示“Java”或“Java EE”的表达式
        Expression isJava = getJavaExpression();
        // 获取表示“Java”且“Python”的表达式
        Expression isJavaAndPython = getJavaAndPythonExpression();

        // 测试表达式是否匹配给定的上下文
        System.out.println("Does John know Java? " + isJava.interpret("John knows Java"));
        System.out.println("Does John know Java and Python? " + isJavaAndPython.interpret("John knows Java and Python"));
    }

    /**
     * 获取表示“Java”或“Java EE”的表达式
     * @return 表达式对象
     */
    public static Expression getJavaExpression() {
        Expression java = new TerminalExpression("Java");
        return new OrExpression(java, new TerminalExpression("Java EE"));
    }

    /**
     * 获取表示“Java”且“Python”的表达式
     * @return 表达式对象
     */
    public static Expression getJavaAndPythonExpression() {
        Expression java = new TerminalExpression("Java");
        Expression python = new TerminalExpression("Python");
        return new AndExpression(java, python);
    }
}